﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Messaging;

namespace ServidorCobro
{
    class QueueServer
    {
        string LocalServerName;
        string IpGestorClientes;
        string RemoteQueueName;

        public QueueServer()
        {
            LocalServerName = ConfigurationManager.AppSettings["LocalServer"];
            IpGestorClientes = ConfigurationManager.AppSettings["IPGestor"];
            RemoteQueueName = ConfigurationManager.AppSettings["RemoteQueue"];
        }

        public void SendMessage(string label, string mensaje)
        {
            using (var queue = new MessageQueue(@"Formatname:DIRECT=TCP:" + IpGestorClientes + "\\Private$\\" + RemoteQueueName))
            {
                var message = new Message(mensaje);
                message.Label = label;
                message.Formatter = new BinaryMessageFormatter();
                queue.Send(message);
            }
        }

        /*public void ReceiveMessage()
        {
            while (true)
            {
                string queueName = @LocalServerName + "\\Private$\\" + LocalQueueName;
                if (MessageQueue.Exists(queueName))
                {
                    using (MessageQueue colaLocal = new MessageQueue(queueName))
                    {
                        //colaLocal.Formatter = new BinaryMessageFormatter();
                        //Message msg = colaLocal.Receive();
                        //msg.ToString();
                        //Thread HiloAccion = new Thread(AccionMensaje);
                        //HiloAccion.Start(msg);
                    }
                }
                else
                {
                    using (MessageQueue colaLocal = MessageQueue.Create(queueName))
                    {
                        //colaLocal.Formatter = new BinaryMessageFormatter();
                        //Message msg = colaLocal.Receive();
                        //msg.ToString();
                        //Thread HiloAccion = new Thread(AccionMensaje);
                        //HiloAccion.Start(msg);
                    }
                }
            }
        }*/

        private void AccionMensaje(object mensaje)
        {
            //Message Mensaje = (Message)mensaje;
            //string Label = Mensaje.Label.ToString();
            //string Datos = Mensaje.Body.ToString();
            //CommServer.EnviaRespuestaAgente(Label, Datos);
        }
    }
}
